package service;

import dao.LabTestDao;
import model.Detail;
import model.LabTest;
import model.User;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import report.IOException_Exception;
import report.LabReport;
import report.LabReportImplService;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: TM
 * Date: 11/25/12
 * Time: 8:39 PM
 * To change this template use File | Settings | File Templates.
 */
public class LabTestReader {
    private LabTestDao labTestDao;

    public void retrieveLabtest(User usr) throws IOException_Exception, ParserConfigurationException, IOException, SAXException {


        LabReportImplService helloService = new LabReportImplService();
        LabReport hello = helloService.getLabReportImplPort();
        String xml = hello.sendLabReport("manoj");
        System.out.println(xml);

        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        List<LabTest> labTestList = new ArrayList<LabTest>();
        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("labTest");
        for (int x = 0; nodes.getLength() > x; x++) {
            LabTest labTest = new LabTest();
            List<Detail> detailList = new ArrayList<Detail>();
            Node node = nodes.item(x);
            NodeList testInfoList = node.getChildNodes();
            for (int cnt = 0; testInfoList.getLength() > cnt; cnt++) {
                Node testInfo = testInfoList.item(cnt);
                System.out.println(testInfo.getNodeName());
                if ("invoiceNo".equalsIgnoreCase(testInfo.getNodeName())) {
                    labTest.setInvoiceNo(testInfo.getNodeValue());
                } else if ("profile".equalsIgnoreCase(testInfo.getNodeName())) {
                    labTest.setProfile(testInfo.getNodeValue());
                } else if ("hospitalCode".equalsIgnoreCase(testInfo.getNodeName())) {
                    labTest.setHospitalCode(testInfo.getNodeValue());
                } else if ("txnDate".equalsIgnoreCase(testInfo.getNodeName())) {
                    labTest.setTxnDate(testInfo.getNodeValue());
                } else if ("test".equalsIgnoreCase(testInfo.getNodeName())) {
                    NodeList testList = testInfo.getChildNodes();
                    Detail detail = new Detail();
                    for (int tstCnt = 0; testList.getLength() > tstCnt; tstCnt++) {
                        Node test = testList.item(tstCnt);
                        if ("testName".equalsIgnoreCase(test.getNodeName())) {
                            detail.setTestName(test.getNodeValue());
                        } else if ("result".equalsIgnoreCase(test.getNodeName())) {
                            detail.setResult(test.getNodeValue());
                        } else if ("referenceRange".equalsIgnoreCase(test.getNodeName())) {
                            detail.setReferenceRange(test.getNodeValue());
                        } else if ("flag".equalsIgnoreCase(test.getNodeName())) {
                            detail.setFlag(test.getNodeValue());
                        }
                    }

                    detailList.add(detail);
                }
            }

            labTest.setDetail(detailList);
            labTestList.add(labTest);
        }

        System.out.println(labTestList);
        for (LabTest labTest : labTestList) {
            labTestDao.saveLabTestResult(usr, labTest);
        }
    }


    public LabTestDao getLabTestDao() {
        return labTestDao;
    }

    public void setLabTestDao(LabTestDao labTestDao) {
        this.labTestDao = labTestDao;
    }
}
